package rabbit.publishsubscribe;

import com.rabbitmq.client.*;
import rabbit.workqueues.WorkQueue_Producer;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Author 汪洋
 * @Description
 */
public class PubSub_Resumer2 {
    public static void main(String[] args) throws IOException, TimeoutException {
        //1、构建一个连接工厂对象
        ConnectionFactory factory = new ConnectionFactory();
        //2、对该工厂对象进行相关的配置
        factory.setUsername("root");
        factory.setPassword("root");
        factory.setVirtualHost("/pjedu");
        //3、通过该工厂获取到连接对象
        Connection connection = factory.newConnection();
        //4、通过连接对象来获取到频道对象
        Channel channel = connection.createChannel();
        //5、构建一个consumer对象
        Consumer consumer = new DefaultConsumer(channel){
            //7、一旦监听到了该队列里面有信息,就执行该方法
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope,
                                       AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("body:"+new String(body));
            }
        };
        //6、监听指定的队列
        channel.basicConsume(PubSub_Producer.QUEUE_NAME_TWO, true,consumer);
    }
}
